package com.lcq.study.leetcode;

/**
 * @ClassName T20210526
 * @Description
 * @Author lichuanqi
 * @Date 2021/5/26 14:08
 * @Version 1.0
 **/
public class T20210526 {
    public static void main(String[] args) {
        int[] nums = {1, 5, 2};
        System.out.println(predict(nums));
    }

    //public static int[] result = new int[2];
    public static int result1 = 0;
    public static int result2 = 0;

    public static boolean predict(int[] nums) {
        int start = 0;
        int end = nums.length - 1;
        // 2 8 10 9 4
        return predict1Step(nums, start, end, 0);
    }

    public static boolean predict1Step(int[] nums, int start, int end, int result22) {
        if (start >= end) {
            return result1 + nums[start] > result22;
        }
        int resultBack = result2;
        result2 = result22;
        if (predict2Step(nums, start + 1, end, result1 + nums[start]) ||
                predict2Step(nums, start, end - 1, result1 + nums[end])) {
            return true;
        } else {
            result2 = resultBack;
            return false;
        }


    }

    public static boolean predict2Step(int[] nums, int start, int end, int result11) {
        if (start >= end) {
            return result11 > result2 + nums[start];
        }
        int resultBack = result1;
        result1 = result11;
        if (predict1Step(nums, start + 1, end, result2 + nums[start]) ||
                predict1Step(nums, start, end - 1, result2 + nums[end])) {
            return true;
        } else {
            result1 = resultBack;
            return false;
        }
    }
}
